package org.dreamwork.modbus.test;

import org.dreamwork.modbus.Predefined;
import org.dreamwork.modbus.tcp.IServerHandler;

import java.util.HashMap;
import java.util.Map;

public class AssignableHandler implements IServerHandler {
    private Map<Integer, Object> data = new HashMap<> ();
    
    public void set (int code, Object value) {
        data.put (code, value);
    }

    @SuppressWarnings ("unchecked")
    public<T> T get (int code) {
        return (T) data.get (code);
    }

    @Override
    public boolean[] onReadCoils (int address, int count) {
        return get (Predefined.Function.Read_Coils);
    }

    @Override
    public boolean[] onReadDiscreteInputs (int address, int count) {
        return get (Predefined.Function.Read_Discrete_Inputs);
    }

    @Override
    public int[] onReadHoldingRegisters (int address, int count) {
        return get (Predefined.Function.Read_Holding_Registers);
    }

    @Override
    public int[] onReadInputRegisters (int address, int count) {
        return get (Predefined.Function.Read_Input_Registers);
    }

    @Override
    public void onWriteSingleCoil (int address, boolean value) {
        set (Predefined.Function.Write_Single_Coil, value);
    }

    @Override
    public void onWriteSingleRegister (int address, int value) {
        set (Predefined.Function.Write_Single_Register, value);
    }

    @Override
    public void onWriteMultipleCoils (int address, boolean[] values) {
        set (Predefined.Function.Write_Multiple_Coils, values);
    }

    @Override
    public void onWriteMultipleRegisters (int address, int[] values) {
        set (Predefined.Function.Write_Multiple_Registers, values);
    }
}
